package com.golxen.framework.common.server.cross.cmd.session;


import com.golxen.framework.CrossProtocol;
import com.golxen.framework.PBPacket.PBPacket;
import com.golxen.framework.common.server.game.cmd.Cmd;
import com.golxen.framework.common.server.game.cmd.Command;
import com.golxen.framework.netty.NettyServices;
import com.golxen.framework.netty.session.base.NettyChannelSession;
import com.golxen.framework.netty.session.base.ServerLinkedGroup;
import com.golxen.framework.netty.session.cross.CrossAgentLinkedSet;
import com.golxen.protojava.LoadProto;
import com.golxen.utils.YmlUtil;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = CrossProtocol.C_REGISTER_CROSS, desc = "注册服务器")
public class RegisterCrossServerCmd implements Command {

	@Override
	public Tuple execute(ChannelHandlerContext ctx, PBPacket packet) throws Exception {
		LoadProto.LoadCrossMsg reqMsg = LoadProto.LoadCrossMsg.parseFrom(packet.toByteArray());
		long serverId = reqMsg.getServerId();
		int serverIndex = reqMsg.getServerIndex();
		int linkIndex = reqMsg.getLinkIndex();

		NettyChannelSession session = new NettyChannelSession(ctx.channel(),   linkIndex);
		CrossAgentLinkedSet.init(serverId, YmlUtil.getInt("netty.cross.agentCount"));
		ServerLinkedGroup group = CrossAgentLinkedSet.addAgentLinkedGroup(serverId, serverIndex, session,
				NettyServices.LINK_COUNT, linkIndex);
		ctx.channel().attr(NettyServices.LINKED_CLIENT).set(group);

		return new Tuple(null, reqMsg);
	}



}
